package com.cgfay.facedetect.engine;

import android.content.Context;
import android.util.Log;

import com.cgfay.facedetect.engine.facepluslus.FaceTracker;
import com.cgfay.facedetect.engine.mediapipes.MediaPipeFaceTracker;
import com.cgfay.facedetect.listener.FaceTrackerCallback;

/**
 * 人脸检测构建器
 */
public final class FaceTrackerBuilder {
    public static final String TAG = FaceTrackerBuilder.class.getSimpleName();

    private IFaceTracker mFaceTracker;
    private FaceTrackParam mFaceTrackParam;

    private static int trackEngineType = 0;

    public static final int FACE_TRACKER_TYPE_FACE_PULS_PLUS = 1;
    public static final int FACE_TRACKER_TYPE_MEDIA_PIPE = 2;
    private static boolean authed = false;


    /****
     * 验证，并设置面部检测器类型
     * @param type FACE_TRACKER_TYPE_FACE_PULS_PLUS or FACE_TRACKER_TYPE_MEDIA_PIPE
     */
    public static void auth(int type, Context context) {
        synchronized (FaceTrackerBuilder.class) {
            if (authed && (type == FACE_TRACKER_TYPE_FACE_PULS_PLUS ||
                            type == FACE_TRACKER_TYPE_MEDIA_PIPE) ) {
                return;
            }
            if (type != FACE_TRACKER_TYPE_FACE_PULS_PLUS &&
                    type != FACE_TRACKER_TYPE_MEDIA_PIPE) {
                trackEngineType = FACE_TRACKER_TYPE_MEDIA_PIPE;
            } else {
                trackEngineType = type;
            }

            if (type == FACE_TRACKER_TYPE_FACE_PULS_PLUS) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        FaceTracker.requestFaceNetwork(context); //face++ 在线验证
                    }
                }).start();
            } else if (type == FACE_TRACKER_TYPE_MEDIA_PIPE) {
                //mediapipe不需要在线验证
            }
            authed = true;
        }
    }

    private static FaceTrackerBuilder builder;
    private FaceTrackerBuilder() {
        if (trackEngineType == FACE_TRACKER_TYPE_FACE_PULS_PLUS) {
            mFaceTracker = FaceTracker.getInstance();
        } else if (trackEngineType == FACE_TRACKER_TYPE_MEDIA_PIPE) {
            mFaceTracker = MediaPipeFaceTracker.Companion.instance();
        } else {
            Log.e(TAG, "instance error, trackEngineType is " + trackEngineType + " is wrong.");
            mFaceTracker = null;
        }
    }

    public static FaceTrackerBuilder instance(){
        synchronized (FaceTrackerBuilder.class) {
            if (!authed) {
                Log.e(TAG, "instance failed, invoke auth() first.");
                return null;
            }
            if (builder == null) {
                synchronized (FaceTrackerBuilder.class) {
                    if (builder == null) {
                        builder = new FaceTrackerBuilder();
                    }
                }
            }
        }
        return builder;
    }

    public IFaceTracker tracker(){
        return builder.mFaceTracker;
    }

//    public FaceTrackerBuilder(IFaceTracker tracker, FaceTrackerCallback callback) {
//        mFaceTracker = tracker;
//        mFaceTrackParam = FaceTrackParam.getInstance();
//        mFaceTrackParam.trackerCallback = callback;
//    }
    public FaceTrackerBuilder registCallback(FaceTrackerCallback callback) {
        mFaceTrackParam = FaceTrackParam.getInstance();
        mFaceTrackParam.trackerCallback = callback;
        return this;
    }

    /**
     * 准备检测器
     */
    public void initTracker() {
        mFaceTracker.initTracker();
    }

    /**
     * 是否预览检测
     * @param previewTrack
     * @return
     */
    public FaceTrackerBuilder previewTrack(boolean previewTrack) {
        mFaceTrackParam.previewTrack = previewTrack;
        return this;
    }

}
